package config

import (
    "encoding/json"
    "fmt"
    "os"
    "bufio"
    "io"
    "strings"
)



type ServerConfig struct {
    Id          int      `json:"id"`
    Addr        string   `json:"addr"`             //return to client ip, maybe is proxy ip
    PortVal     int   `json:"port"`
}

func (sv *ServerConfig) Address() string {
    return sv.Addr
}

func (sv *ServerConfig) Port() int {
    return sv.PortVal
}

func (sv *ServerConfig) Log() *LogConfig {
    return conf_inc_.GetLogConfig()
}

type LogConfig struct {
    Dir        string  `json:"logdir"`
    Level      string  `json:"loglevel"`
    BufferSize int     `json:"buffersize"`
}

func (sv *LogConfig) LogDir() string {
    return sv.Dir
}

func (sv *LogConfig) LogLevel() string {
    return sv.Level
}

func (sv *LogConfig) LogBuffer() int {
    return sv.BufferSize
}

type DataBaseConfig struct {
    Id        int      `json:"id"`
    Addr      string   `json:"addr"`
    PortVal   int      `json:"port"`
    AddrBk    string   `json:"addr_bk"`
    PortBkVal int      `json:"port_bk"`
}

func (conf *Config) GetServerConfig(idx int) *ServerConfig{
    if len(conf.Servers) > idx {
        return &conf.Servers[idx]
    }

    return nil
}


func (conf *Config) GetServerNum() int {
    return len(conf.Servers)
}

func (conf *Config) GetDataBaseConfig(idx int) *DataBaseConfig{
    if len(conf.DBs) > idx {
        return &conf.DBs[idx]
    }

    return nil
}

func (conf *Config) GetDataBaseNum() int {
    return len(conf.DBs)
}

func (conf *Config) GetLogConfig() *LogConfig {
    return &conf.Log
}


var conf_inc_ *Config = nil

func Instance() *Config {
    if conf_inc_ != nil {
        return conf_inc_
    }

    conf_inc_ = loadServerCFG(server_config_file)

    return conf_inc_
}

func GetServerConfig(idx int) *ServerConfig{
    return conf_inc_.GetServerConfig(idx)
}

func GetDataBaseConfig(idx int) *DataBaseConfig{
    return conf_inc_.GetDataBaseConfig(idx)
}

func GetLogConfig() *LogConfig {
    return &conf_inc_.Log
}

type Config struct {
    Servers []ServerConfig    `json:"server"`
    Log     LogConfig         `json:"log"`
    DBs     []DataBaseConfig  `json:"database"`
}

func loadServerCFG(file string) *Config {
    f, err := os.Open(file)
    if err != nil {
        fmt.Fprintf(os.Stderr, "load File [%s] Error : %s", file, err)
        os.Exit(2)
    }

    content := []byte{}
    fread := bufio.NewReader(f)
    for {
        line, err := fread.ReadString('\n')
        if err != nil && err != io.EOF{
            fmt.Fprintf(os.Stderr, "Read File[%s] error: %s", file, err)
            os.Exit(2)
        }

        idx := strings.Index(line, "//")
        if idx == -1 {
            content = append(content, line[:]...)
        } else {
            content = append(content, line[:idx]...)
            content = append(content, '\n')
        }

        if err == io.EOF {
            break
        }
    }

    //fmt.Println("content : ", string(content))

    cfg := new (Config)
    err = json.Unmarshal(content, cfg)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unmarshal Json [%s] Error : %s", file, err)
        os.Exit(2)
    }

    return cfg
}

const server_config_file = "./config/server.json"

func init() {
    conf_inc_ = loadServerCFG(server_config_file)
}
